package com.elroy_zhongxing.spirit_elroy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements View.OnClickListener {
    private Button btn_login;
    private Button btn_logout;
    private EditText et_account;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initUI();
    }

    /**
     * 初始化控件绑定
     */
    private void initUI() {
        btn_login = (Button) findViewById(R.id.btn_login);
        //添加点击事件
        btn_login.setOnClickListener(/*匿名内部类*/new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框内容
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();


                //数据合法性验证
                if (account.trim().length() < 5) {
                    ToastUtil.toast(getApplicationContext(), "账号至少要求5位");
                    return;
                }
                if (password.trim().length() < 3) {
                    ToastUtil.toast(getApplicationContext(), "密码至少要求3位");
                    return;
                }

                if ("admin".equals(account) && "123".equals(password)) {
                    //跳转到主界面
                    Intent intent = new Intent();//创建意图
                    intent.setClass(getApplicationContext(), MainActivity.class);
                    //执行意图
                    startActivity(intent);
                }else {
                    ToastUtil.toast(getApplicationContext(), "账号或密码错误");
                }

            }
        });//代理模式
        btn_logout = (Button) findViewById(R.id.btn_logout);//继承的多态性
        //添加点击事件
        btn_logout.setOnClickListener(this);//接口的多态性

        et_account = (EditText) findViewById(R.id.User_Name);
        et_password = (EditText) findViewById(R.id.Password);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_logout:
                Toast.makeText(getApplicationContext(), "登出", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}